package pl.register.android.widget.button;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;
import pl.register.android.rating.Rating;
import pl.register.android.util.CompositeOnClickListener;

/**
 * @author Mateusz Strzelecki
 * @since 2013-04-21
 */
public class RatingButton extends ImageButton implements View.OnClickListener {

    private static final Rating DEFAULT_RATING = Rating.NONE;

    private Rating rating;

    public RatingButton(Context context) {
        super(context);
        setupButton();
    }

    public RatingButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        setupButton();
    }

    private void setupButton() {
        rating = DEFAULT_RATING;
        setImageResource(rating.getStarResId());
        super.setOnClickListener(this);
    }

    public Rating getRating() {
        return rating;
    }

    public void setRating(Rating rating) {
        this.rating = rating;
        setImageResource(rating.getStarResId());
    }

    @Override
    public void setOnClickListener(OnClickListener listener) {
        CompositeOnClickListener listeners = new CompositeOnClickListener(this, listener);
        super.setOnClickListener(listeners);
    }

    @Override
    public void onClick(View view) {
        setRating(rating.getNext());
    }
}
